﻿Imports Microsoft.Win32

''' <summary>
''' Action which opens a project.
''' </summary>
''' <remarks></remarks>
''' <editHistory></editHistory>
Public Class OpenAction

    Public Shared Function CanExecute() As Boolean '
        Return True
    End Function

    ''' <summary>
    ''' Executes the action.
    ''' </summary>
    ''' <param name="project">This is the project that was opened.</param>
    ''' <param name="filename">This is the filename that was used to open the project.</param>
    ''' <returns></returns>
    Public Shared Function Execute(ByRef project As ScriptProject, _
                                   ByRef filename As String) As Boolean

        If String.IsNullOrEmpty(filename) Then

            'Create the dialog
            Dim dialog As New OpenFileDialog

            'Set the filter
            dialog.Filter = Constants.ProjectFileFilter

            'Show the dialog
            Dim result As Boolean? = dialog.ShowDialog()

            'Check the result
            If result.HasValue AndAlso result.Value Then

                'Attempt to open it
                project = ScriptProject.Open(dialog.FileName)

                'Save the filename
                filename = dialog.FileName

                'We're good
                Return True
            Else
                'Nah, didn't work out
                Return False
            End If

        Else
            'Attempt to open it
            project = ScriptProject.Open(filename)

            Return True
        End If

    End Function

End Class
